#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QKeyEvent>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QFont>
#include <QLabel>
#include <QWidget>
#include <QMouseEvent>
#include <QListWidget>
#include <QStackedWidget>
#include <QByteArray>
#include <unistd.h>
#include <QMenu>
#include <QTimer>
#include <QSettings>
#include "titlebar.h"
#include "widgetstyle.h"
#include "button.h"
#include "camerapage.h"
#include "pictureviewpage.h"
#include "setting.h"
#include "VideoDisplay.h"
#include "kylincamera_global.h"
#include "KylinCamera.h"
#include "deviceread.h"
#include <QDateTime>
#include <QStandardPaths>
#include <QGSettings/QGSettings>
#include <QPixmap>
#include "albumread.h"
#include "settingpage.h"
#include "aboutwidget.h"
#include <QProcess>
#include <QDBusUnixFileDescriptor>
#include <QMutex>
#include "util/login1_dbus.h"
#include "cameracodestate.h"
class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow(QStringList strList,QWidget *parent = nullptr);
    ~MainWindow();
    //设置通用UI
    void setCommonUI();
    //应用置顶
    void stayTop();
    //点击设置按钮
    void funcListItemClicked(QListWidgetItem* item);

    void setbutton();
    void setcamerapage();
    void setpicturepage();
    void loadPhoto();

    //声明配置文件
//    QSettings *picture_setting = new QSettings(QCoreApplication::applicationDirPath() + "/.configure/kylin-camera/picture_setting");
      static QGSettings *setting;
//    static QSettings *setting;
//    QSettings *isVideo_setting = new QSettings(QCoreApplication::applicationDirPath() + "/.configure/kylin-camera/isVideo_setting");
    QPoint temp;
    bool settingShow = false;
    static int picture_number;
    static bool mirrorFlag;   //lhl debug
signals:
    void click_video();
    void click_picture();

public slots:
    int kylinCameraDbusRequest(QStringList command);

private slots:

    void takePhoto(QString name, bool isvideo);

    QString creatName();
    QString trueName(QString name);
    void startCommend(QString cmd);

    void clickPause();
    //  void clickRestore();
    void clickPhoto();
    void clickStartRecord();
    void clickStopRecord();
    void clickDelay();
    void itemclicked(QListWidgetItem*);
    void itemDoubleClicked(QListWidgetItem*);
    void pictureview(bool flag = true);
    void imageDisplay(QString filename);
    void delete_picture();
    void save_dir_change();
    void timeEvent();
    void setMaxWindow(bool);
    void updateRecordTime();
    void imageView(QString,bool);
    void mirror(bool);
    void triggerMenu(QAction*);
    void listWidgetUpdate(int);
    void noPathPrompt();   //lhl debug
    void noPathHandle();   //lhl debug
    void settingPathPrompt(); //lhl debug
    //异常处理
    void exception_handing();
    void triggerThemeMenu(QAction *);

    void onPrepareForShutdown(bool start);
    void onPrepareForSleep(bool start);

    void delay_operation(int);
protected:
    virtual void mousePressEvent(QMouseEvent *);
    virtual void mouseMoveEvent(QMouseEvent *);
    virtual void mouseReleaseEvent(QMouseEvent *);
    virtual void enterEvent(QEvent *event);
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void paintEvent(QPaintEvent *event);
    virtual void resizeEvent(QResizeEvent *event);
    virtual bool eventFilter(QObject *watched, QEvent *event);
    virtual void mouseDoubleClickEvent(QMouseEvent *event);
private:
    bool check_save_path();
    void inhibit(QString what,QString why);
    void unInhibit();
    //void initCommandDbus();
    bool singleInstance(QStringList strList);
    void initDbus();
private:
    Qt::WindowFlags winFlags;
    QStringList::reverse_iterator iter;
    QStringList picture_list;
    QString fileNameTemp;  //照片名字
    QWidget *ViewWid;
    QWidget *PictureWid;
    QWidget *btnWid;
    QWidget *titleBarWid = nullptr;
    //  QWidget *setWid;
    QWidget *rightbtn;
    QWidget *mainWid;

    QHBoxLayout *btnLayout = nullptr;
    QHBoxLayout *mainLayout = nullptr;
    QGridLayout *pictureLayout = nullptr;

    DeviceRead *devRead;
    //各个组件
    Setting *setWid = nullptr;
    CameraPage *camerapage = nullptr;
    TitleBar *pTitleBar = nullptr;
    PictureViewPage *viewpage;
    SettingPage *setPage;
    AboutWidget *aboutWinWidget;


    QString imagePath; //保存路径
    QString configPath; //储存中间文件的目录

    //监听设备文件
    QFileSystemWatcher *watcher;

    QTimer *timer = nullptr;
    //倒计时
    QWidget *dead_time;
    QProcess *proc;
    QPoint m_start;
    QPoint m_end;
//    void changeEvent(QEvent *);

    int index = -1;
    int timerId;
    int dead_time_sec;
    int dead_time_sec_tmp = 0;
    bool is_video = false;
    bool Burst_mode = false;
    bool press = false;
    bool if_delay = false;
    bool m_needContinue = false;

    login1dbusManager *m_dbus;
    QDBusUnixFileDescriptor m_inhibitFileDescriptor;
    DaemonIpcDbus *m_dbusHanler;
    QString m_currentUser;
    QMutex m_mutex;
private slots:
    void onSigTerm();
    void onSigHup();
    void onSigAbrt();
    void onLocked();
    void onUnlocked();
    void onShutDown();
    //"true" means system is going into "sleep,lock" state.
    //"false" means system is awake or unlock.
    void recordingInterrupt(bool in);
    void recordInterrupt();
    void recordInterruptDone();

};
#endif // MAINWINDOW_H
